<template>
  <div class="">
    <div class="module-card">
      <h3>题目描述：</h3>
      <div>{{ title }}</div>
    </div>
    <div class="module-card">
      <h3>题目要求：</h3>
      <div
        :class="{ 'margin-top': index !== 0 }"
        v-for="(item, index) in detail"
        :key="index"
      >
        <div class="">{{ item.des }}</div>
        <div v-if="item.example" class="">{{ item.example }}</div>
      </div>
    </div>
    <div class="module-card">
      <input v-model="valueStr" @change="handleChange" />
      <div class="margin-top">结果是：</div>
      <div>行一：{{ resultInfo.line1 }}</div>
      <div>行二：{{ resultInfo.line2 }}</div>
    </div>
  </div>
</template>

<script>
export default {
  props: {},
  components: {},
  data() {
    return {
      title:
        "幼儿园两个班的小朋友在排队时混在了一起，每位小朋友都知道自己是否与前面一位小朋友是否同班，请你帮忙把同班的小朋友找出来。小朋友的编号为整数，与前一位小朋友同班用Y表示，不同班用N表示。",
      detail: [
        {
          des: "输入为：空格分开的小朋友编号和是否同班标志",
          example:
            "比如：6/N 2/Y 3/N 4/Y，表示共4位小朋友，2和6同班，3和2不同班，4和3同班。其中，小朋友总数不超过999，每个小朋友编号大于0，小于等于999。不考虑输入格式错误问题。",
        },
        {
          des: "输出为：两行，每一行记录一个班小朋友的编号，编号用空格分开。",
          example: "",
        },
        {
          des: "编号需要按照大小升序排列，分班记录中第一个编号小的排在第一行。",
          example: "",
        },
        { des: "若只有一个班的小朋友，第二行为空行。", example: "" },
        { des: "若输入不符合要求，则直接输出字符串ERROR。", example: "" },
      ],
      valueStr: "6/N 2/Y 3/N 4/Y", // 分班的输入值
      resultInfo: {
        line1: "",
        line2: "",
      },
    };
  },
  created() {},
  mounted() {
    this.handleChange();
  },
  beforeDestroy() {},
  computed: {},
  watch: {},
  methods: {
    handleChange() {
      this.resultInfo.line1 = "";
      this.resultInfo.line2 = "";
      const valueStr = this.valueStr;
      const valueArr = valueStr.split(" ");
      if (valueArr.length > 999) {
        console.log("ERROR");
        return;
      }
      console.log(valueStr, valueArr);
      const arr1 = [];
      const arr2 = [];
      let isError = false;
      for (let i = 0; i < valueArr.length; i++) {
        const p = valueArr[i];
        if (!p.includes("/")) {
          isError = true;
          break;
        }
        const pArr = p.split("/");
        if (pArr[0] <= 0 || pArr[0] > 999) {
          isError = true;
          break;
        }
        if (i === 0) {
          arr1.push(pArr[0]);
          continue;
        }
        const lastP = valueArr[i - 1].split("/");
        const isInArr1 = arr1.includes(lastP[0]);
        const isSameClass = pArr[1] === "Y";
        if (isInArr1) {
          if (isSameClass) {
            arr1.push(pArr[0]);
          } else {
            arr2.push(pArr[0]);
          }
        } else {
          if (isSameClass) {
            arr2.push(pArr[0]);
          } else {
            arr1.push(pArr[0]);
          }
        }
      }
      if (isError) {
        console.log("ERROR");
        return;
      }
      const arrAll = [...arr1, ...arr2].sort(sortArr);
      arr1.sort(sortArr);
      arr2.sort(sortArr);
      const isArr1First = arr1.includes(arrAll[0]);
      if (isArr1First) {
        this.resultInfo.line1 = arr1.join(",");
        this.resultInfo.line2 = arr2.length ? arr2.join(",") : "";
        console.log(this.resultInfo.line1);
        console.log(this.resultInfo.line2);
      } else {
        this.resultInfo.line1 = arr2.join(",");
        this.resultInfo.line2 = arr1.join(",").length ? arr1.join(",") : "";
        console.log(this.resultInfo.line1);
        console.log(this.resultInfo.line2);
      }

      // 升序
      function sortArr(a, b) {
        return a - b;
      }
    },
  },
};
</script>

<style lang="less"></style>

<style lang="less" scoped>
.module-card {
  > h3 {
    font-weight: bolder;
  }
}
.margin-top {
  margin-top: 20px;
}
</style>
